<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>快速开发平台</h2>
        <p>欢迎登录</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="demo-accounts">
        <h4>演示账号：</h4>
        <div class="account-list">
          <div class="account-item" @click="fillAccount('admin', '123456')">
            <strong>管理员：</strong> admin / 123456
          </div>
          <div class="account-item" @click="fillAccount('user1', '123456')">
            <strong>普通用户：</strong> user1 / 123456
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance } from 'element-plus'
import { useUserStore } from '@/stores/user'
import type { LoginRequest } from '@/types'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive<LoginRequest>({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const result = await userStore.loginAction(loginForm)
        if (result.success) {
          ElMessage.success('登录成功')
          
          // 登录成功后，使用 replace 跳转到dashboard，避免路由历史问题
          router.replace('/dashboard')
        } else {
          ElMessage.error(result.message || '登录失败')
        }
      } catch (error: any) {
        ElMessage.error(error.message || '登录失败')
      } finally {
        loading.value = false
      }
    }
  })
}

const fillAccount = (username: string, password: string) => {
  loginForm.username = username
  loginForm.password = password
}
</script>

<style lang="less" scoped>
// 定义变量
@primary-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
@box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
@text-color-dark: #333;
@text-color-medium: #666;
@background-light: #f5f7fa;
@border-radius-large: 10px;
@border-radius-small: 5px;
@border-radius-mini: 3px;

// 登录容器
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: @primary-gradient;

  // 登录框
  .login-box {
    width: 400px;
    padding: 40px;
    background: white;
    border-radius: @border-radius-large;
    box-shadow: @box-shadow;

    // 登录头部
    .login-header {
      text-align: center;
      margin-bottom: 30px;

      h2 {
        color: @text-color-dark;
        margin-bottom: 10px;
      }

      p {
        color: @text-color-medium;
        font-size: 14px;
      }
    }

    // 登录表单
    .login-form {
      margin-bottom: 20px;
    }

    .login-button {
      width: 100%;
    }

    // 演示账号区域
    .demo-accounts {
      margin-top: 20px;
      padding: 15px;
      background: @background-light;
      border-radius: @border-radius-small;

      h4 {
        margin: 0 0 10px 0;
        color: @text-color-dark;
        font-size: 14px;
      }

      .account-list {
        font-size: 12px;

        .account-item {
          margin-bottom: 8px;
          padding: 5px;
          cursor: pointer;
          border-radius: @border-radius-mini;
          transition: background-color 0.2s;

          &:hover {
            background-color: #e1e6f0;
          }

          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }
  }
}
</style>